/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.qp.smsmarketing.dao.abstractdao;

import com.qp.smsmarketing.entity.Grupo;
import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 *
 * @author waldenilson
 */
public abstract class AbstractDAO implements IDAO {

    protected SessionFactory sf;
    protected Session session;
    protected Transaction transaction;
    protected EntityManager entityManager;
    protected Query query;

    // abstract methods

    protected List findAllRows(String entityClass)
    {
        //if(session == null)
          //  session = sf.openSession();
        //List l = session.createQuery("FROM "+entityClass).list();
        
        List l = entityManager.createQuery("FROM "+entityClass).getResultList();

        return l;
    }
    protected boolean salvar(Object o)
    {
        try
        {
//            if(session == null)
//                session = sf.openSession();
//            session.save(o);
//            transaction = session.beginTransaction();
//            transaction.commit();
//            session.close();
            entityManager.getTransaction().begin();
            entityManager.persist(o);
            entityManager.getTransaction().commit();


            return true;
        }
        catch(Exception e)
        {
            System.out.println(e);
            return false;
        }
    }
    protected boolean atualizar(Object o)
    {
        try
        {
//           if(session == null)
//                session = sf.openSession();
//            transaction = session.beginTransaction();
//            session.merge(o);
//            transaction.commit();
//            session.close();

            entityManager.getTransaction().begin();
            entityManager.merge(o);
            entityManager.getTransaction().commit();

            return true;
        }
        catch(Exception e)
        {
            System.out.println(e);
            return false;
        }
    }
}
